package com.zhuojy.equity.mall.admin.manager.bo;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

import com.zhuojy.equity.mall.admin.common.constant.Constants;
import com.zhuojy.equity.mall.admin.dal.dataobject.TResourceDO;
import com.zhuojy.equity.mall.admin.dal.dataobject.TUserDO;
import lombok.RequiredArgsConstructor;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

/**
 * SpringSecurity需要的用户信息封装类
 */
@RequiredArgsConstructor
public class AdminUserDetails implements UserDetails {

	private static final long serialVersionUID = -4470332789701334732L;

	//后台用户
	private final TUserDO userDO;

	//拥有资源列表
	private final List<TResourceDO> resourceList;

	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		//返回当前用户所拥有的资源
		return resourceList.stream()
				.map(resource -> new SimpleGrantedAuthority(resource.getId() + ":" + resource.getName()))
				.collect(Collectors.toList());
	}

	@Override
	public String getPassword() {
		return userDO.getPassword();
	}

	@Override
	public String getUsername() {
		return userDO.getUsername();
	}

	@Override
	public boolean isAccountNonExpired() {
		return true;
	}

	@Override
	public boolean isAccountNonLocked() {
		return true;
	}

	@Override
	public boolean isCredentialsNonExpired() {
		return true;
	}

	@Override
	public boolean isEnabled() {
		return userDO.getState().equals(Constants.OPEN);
	}
}
